package com.aitaoxue.palx.ui;

import io.rong.imkit.RongIM;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.InjectView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.api.remote.AitaoXueApi;
import com.aitaoxue.palx.base.BaseActivity;
import com.aitaoxue.palx.bean.RobOrder;
import com.aitaoxue.palx.bean.SearchParent;
import com.aitaoxue.palx.util.GsonUtil;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class RockOrderDetailActivity extends BaseActivity {

	@InjectView(R.id.name)
	TextView name;
	@InjectView(R.id.price)
	TextView price;
	@InjectView(R.id.watch_num)
	TextView watchNum;
	@InjectView(R.id.time)
	TextView time;
	@InjectView(R.id.address)
	TextView address;
	@InjectView(R.id.desc)
	TextView desc;
	@InjectView(R.id.chat)
	Button chat;
	@InjectView(R.id.at)
	Button at;

	SearchParent parent;

	private String phone = null;

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	protected int getActionBarTitle() {
		return R.string.pull_order_detail;
	}

	@Override
	protected int getLayoutId() {
		return R.layout.pull_order_detail;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.chat:
			if (phone != null && RongIM.getInstance() != null) {
				RongIM.getInstance().startPrivateChat(
						RockOrderDetailActivity.this, phone, "test");
			}
			break;
		case R.id.at:
			showWaitDialog();
			List<RobOrder> list = new ArrayList<RobOrder>();
			list.add(new RobOrder(parent.getRequirement_id()));
			String params = GsonUtil.ListToJson(list);

			AitaoXueApi.connectParentList(params, mHandler);
			break;
		default:
			break;
		}
	}

	@Override
	public void initView() {
		parent = (SearchParent) getIntent().getSerializableExtra("detail");

		AitaoXueApi.addVisitorNum(parent.getRequirement_id(), mHandler2);

		name.setText(parent.getName());
		price.setText(parent.getPrice() + "元/课时");
		watchNum.setText(parent.getRequirement_visitor() + "人已浏览");
		time.setText(parent.getPost_time() + "发布");
		address.setText(parent.getCity_name());
		desc.setText(parent.getDesc());

		chat.setOnClickListener(this);
		at.setOnClickListener(this);
	}

	@Override
	public void initData() {

	}

	private AsyncHttpResponseHandler mHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			String res = new String(arg2);
			hideWaitDialog();

			try {
				JSONObject json = new JSONObject(res);
				String code = json.getString("code");
				if (code.equals("0000")) {
					Toast.makeText(RockOrderDetailActivity.this, "联系成功",
							Toast.LENGTH_SHORT).show();
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {

		}
	};

	private AsyncHttpResponseHandler mHandler2 = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			String res = new String(arg2);

			try {
				JSONObject json = new JSONObject(res);

				String tmp = json.getString("content");

				JSONObject obj = new JSONObject(tmp);

				phone = obj.getString("user_phone");
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {

		}
	};

}
